﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace BOSS.Web
{
    /// <summary>
    /// 自定义的 ViewPage 基类，提供一些常用函数
    /// </summary>
    public class BaseViewPage : ViewPage
    {
        private bool m_even = true;

        private int m_index = 0;

        /// <summary>
        /// 获取 表格行的样式名称
        /// </summary>
        public string RowClass
        {
            get
            {
                m_even = !m_even;
                if (m_even)
                {
                    return "even";
                }
                else
                {
                    return "odd";
                }
            }
        }

        /// <summary>
        /// 获取记录索引
        /// </summary>
        public int Index
        {
            get
            {
                return ++this.m_index;
            }
        }

        /// <summary>
        /// 重置表格行样式名称
        /// </summary>
        public void ResetRowClass()
        {
            this.m_even = true;
        }

        /// <summary>
        /// 重置记录索引
        /// </summary>
        public void ResetIndex()
        {
            this.m_index = 0;
        }

        /// <summary>
        /// 判断是否拥有指定页面许可
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool IsAllow(string permission)
        {
            IDictionary<string, bool> permissions = ViewData[ViewDataConstants.PERMISSIONS] as IDictionary<string, bool>;
            if (permissions != null)
            {
                if (permissions.ContainsKey(permission))
                {
                    return permissions[permission];
                }
                return false;
            }
            return false;
        }

        /// <summary>
        /// 判断是否不拥有指定页面许可
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool IsDeny(string permission)
        {
            return !IsAllow(permission);
        }
    }

    /// <summary>
    /// 自定义的 ViewPage 基类，提供一些常用函数
    /// </summary>
    /// <typeparam name="TModel">TModel is model</typeparam>
    public class BaseViewPage<TModel> : ViewPage<TModel> where TModel : class
    {
        private bool m_even = true;

        private int m_index = 0;

        /// <summary>
        /// 获取 表格行的样式名称
        /// </summary>
        public string RowClass
        {
            get
            {
                m_even = !m_even;
                if (m_even)
                {
                    return "even";
                }
                else
                {
                    return "odd";
                }
            }
        }

        /// <summary>
        /// 获取记录索引
        /// </summary>
        public int Index
        {
            get
            {
                return ++this.m_index;
            }
        }

        /// <summary>
        /// 重置表格行样式名称
        /// </summary>
        public void ResetRowClass()
        {
            this.m_even = true;
        }

        /// <summary>
        /// 重置记录索引
        /// </summary>
        public void ResetIndex()
        {
            this.m_index = 0;
        }

        /// <summary>
        /// 判断是否拥有指定页面许可
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool IsAllow(string permission)
        {
            IDictionary<string, bool> permissions = ViewData[ViewDataConstants.PERMISSIONS] as IDictionary<string, bool>;
            if (permissions != null)
            {
                if (permissions.ContainsKey(permission))
                {
                    return permissions[permission];
                }
                return false;
            }
            return false;
        }

        /// <summary>
        /// 判断是否不拥有指定页面许可
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        public bool IsDeny(string permission)
        {
            return !IsAllow(permission);
        }
    }
}
